package com.bwie.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang3.ObjectUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.GroupOrder;
import com.bwie.mapper.GroupOrderMapper;
import com.bwie.service.GroupOrderService;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

@Service
@Transactional
public class GroupOrderServiceImpl extends ServiceImpl<GroupOrderMapper, GroupOrder> implements GroupOrderService{
    @Autowired
    private GroupOrderMapper groupOrderMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public R getGroupOrderList(PageInfoVo pageInfoVo) {
        Page<GroupOrder> groupOrderPage = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        String token = request.getHeader("token");
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        Integer userId = body.get("userId", Integer.class);
        Page<GroupOrder> groupOrderPage1 = groupOrderMapper.getGroupOrderList(groupOrderPage,pageInfoVo,userId);
        return R.OK(groupOrderPage1);
    }

    @Override
    public R deleteOrder(Integer orderId) {
        GroupOrder groupOrder = groupOrderMapper.selectById(orderId);
        if(ObjectUtils.isEmpty(groupOrder)){
            return R.ERROR(500,"当前订单不存在");
        }
        groupOrderMapper.deleteById(orderId);
        return R.OK();
    }
}
